package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {


    /**
     * 添加 和 编辑
     *
     * @param checkGroupDTO
     * @return
     */

    @Autowired
    private CheckItemService checkItemService;

    @Override
    public Boolean saveCheckGroupInfo(CheckGroupDTO checkGroupDTO) {

        // 执行过添加方法后，自动生成主键，所有DTO对象里面就会有保存的主键
        Integer id = checkGroupDTO.getId();

        if(StringUtils.isNotBlank(String.valueOf(id))){
            //  根据 id  删除 中间表数据
            baseMapper.deleteCheckItemInfoByGroupId(id);
        }
        boolean flag = saveOrUpdate(checkGroupDTO);//   完成检查组录入  自动生成主键
        Integer gid = checkGroupDTO.getId();

        //  录入中间表
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.length > 0) {
            //  中间表循环录入
            for (Integer checkitemId : checkitemIds) {
                //   添加和修改  中间数据录入
                baseMapper.addCheckGroupIdAndCheckItemId(gid, checkitemId);
            }
        }
        return flag;
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        // sql * from xxx where is_delete = 0 and xxx like ?
        // 条件构造出来 Wrapper对象
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        Page<CheckGroup> page = null;
        if (StringUtils.isBlank(queryPageBean.getQueryString())) {
            // 无条件分页    page方法是接口默认方法，没有对象也能直接调用，是Java8新特性之一
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        } else {
            queryWrapper.like("name", queryPageBean.getQueryString());
            //   sql   + 追加  sql  where  name like  %?%
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()), queryWrapper);
        }

        PageResult result = new PageResult(page.getTotal(), page.getRecords());
        return result;
    }

    @Override
    public Boolean updateCheckGroupById(int id) {
        // 逻辑删除
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        return updateById(checkGroup);
    }

    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int id) {

        //   查询所有检查项信息  +  gid  查询对应得 checkitemIds 中间表
        List<CheckItem> list = checkItemService.list();
        int[] checkitemIds = baseMapper.findCheckItemInfoByGroupId(id); //  一表
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(list); //  将查询的结果集封装到vo 中
        checkGroupVO.setCheckitemIds(checkitemIds);
        return checkGroupVO;
    }
}
